import { approvalType, userType } from '@/components/MiniBpmn/types';
import UserSelector, { type IUserSelectorView } from '../';
import { type IBaseSelector, type ISelectorOption } from '../selector/base';
import type CardService from '@/components/MiniBpmn/cards/base/service';
import ApprovalTypeService from '../../approvalType/service';
import BpmnControlService from '../../bpmn/service';
import UserService from './selector/user/service';

class ApprovalUserSelectorService extends BpmnControlService<string> implements IUserSelectorView {
  declare parent: CardService;

  label: string = '';
  value: string = '';
  field = 'flowable:dataType';

  selector?: IBaseSelector;

  selectors: ISelectorOption[] = [];
  selectorsMap: Record<string, IBaseSelector> = {};

  constructor({ label, selectors }: { label: string, selectors: ISelectorOption[] }) {
    super({ label });
    this.label = label;
    this.selectors = selectors;
  }

  onChange(value: string) {
    this.value = value;
    this.updateProperties(value);
    this.initSelector(value);
    this.selector?.initData(); // 重置数据
    this.updateApprovalTypeDisplay(value);
    this.updateApprovalTypeProperties();
  }

  /** 更新审批类型是否显示 */
  updateApprovalTypeDisplay(value: string) {
    const control = this.parent.controls.find(item => {
      return item instanceof ApprovalTypeService;
    });
    if (control) {
      // if(this.selector instanceof UserSelector) {
      if ([userType.role, userType.department].includes(value)) {

        control.setVisible(true);
        // 用户数量大于1时，才显示审批类型
      } else if (value === userType.user && this.selector instanceof UserService) {
        if (this.selector.value.length > 1) {
          control.setVisible(true);
        } else {
          control.setVisible(false);
        }
      } else {
        control.setVisible(false);
      }

    }
  }

  /** 更新审批类型的值 */
  updateApprovalTypeProperties() {
    const control = this.parent.controls.find(item => {
      return item instanceof ApprovalTypeService;
    }) as ApprovalTypeService | undefined;
    if (control) {
      // 本控件会缓存之前的属性，根据是否显示来决定清空还是恢复
      if (control.visible) {
        control.updateProperties(control.value);
      } else {
        control.updateProperties(approvalType.none, this.value);
      }
    }
  }

  getSelector(type: string) {
    if (this.selectorsMap[type]) {
      return this.selectorsMap[type];
    } else {
      // 初始化
      const Selector = this.selectors.find(item => item.value === type);

      if (Selector) {
        this.selectorsMap[type] = new Selector.Service();
      } else {
        throw new Error(`未找到${type}对应的选择器`);
      }
      return this.selectorsMap[type];
    }
  }

  // 初始化选择器
  initSelector(value: string) {
    const selector = this.getSelector(value);
    this.selector = selector;
    if (selector) {
      selector.bindBpmnService(this.root);
      selector.bindPanelService(this.panel);
      if (selector instanceof UserService) {
        selector.bindOnChange(() => {
          this.updateApprovalTypeDisplay(value);
          this.updateApprovalTypeProperties();
        });
      }
      selector.init();
      selector?.onShow?.();
    }
  }

  init(): void {
    const type: string = this.panel.getElement().businessObject.$attrs['flowable:dataType'];
    this.value = type;

    if (!this.value) {
      this.onChange(userType.user); // 初始化默认用户选择器
    } else {
      this.initSelector(this.value);
      this.updateApprovalTypeDisplay(this.value);
    }
  }

  onShow(): void {
    this.selector?.onShow?.();
  }

  isShowInitiator: boolean = false;
  component = UserSelector;
}

export default ApprovalUserSelectorService;
